package com.wlm.shortlink.admin.remote;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wlm.shortlink.admin.common.convention.result.Result;
import com.wlm.shortlink.admin.dto.req.RecycleBinSaveReqDTO;
import com.wlm.shortlink.admin.remote.dto.req.*;
import com.wlm.shortlink.admin.remote.dto.resp.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用接口
 */
public interface ShortLinkRemoteSerice {

    /**
     * 创建短链接
     *
     * @param requestParam 创建短链接请求参数
     * @return 短链接创建响应
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam) {
        String resultBodyStr = HttpUtil.post("http://localhost:8001/api/short-link/project/v1/create", JSON.toJSONString(requestParam));

        // TypeReference - JSON反序列化时解析泛型
        return JSON.parseObject(resultBodyStr, new TypeReference<Result<ShortLinkCreateRespDTO>>() {});
    }

    /**
     * 分页查询短链接
     *
     * @param requestParam 分页查询短链接请求参数
     * @return 短链接分页查询响应
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("orderTag",requestParam.getOrderTag());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());

        String resultPageStr = HttpUtil.get("http://localhost:8001/api/short-link/project/v1/page", requestMap);

        // TypeReference - JSON反序列化时解析泛型
        return JSON.parseObject(resultPageStr, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>() {});
    }

    /**
     * 查询分组短链接总数
     *
     * @param requestParam 分组短链接总数请求参数
     * @return 查询分组短链接总数响应
     */
    default Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(List<String> requestParam) {
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String resultPageStr = HttpUtil.get("http://localhost:8001/api/short-link/project/v1/count", requestMap);
        // TypeReference - JSON反序列化时解析泛型
        return JSON.parseObject(resultPageStr, new TypeReference<Result<List<ShortLinkGroupCountQueryRespDTO>>>() {});
    }

    /**
     * 修改短链接
     *
     * @param requestParam 修改短链接请求参数
     * @return 短链接创建响应
     */
    default void updateShortLink(ShortLinkUpdateReqDTO requestParam) {
        HttpUtil.post("http://localhost:8001/api/short-link/project/v1/update", JSON.toJSONString(requestParam));
    }

    /**
     * 根据 URL 获取标题
     *
     * @param url 目标网站地址
     * @return 网站标题
     */
    default Result<String> getTitleByUrl(@RequestParam("url")String url) {
        String resultStr = HttpUtil.get("http://localhost:8001/api/short-link/project/v1/title?url=" + url);
        return JSON.parseObject(resultStr, new TypeReference<>() {});
    }


    /**
     * 保存回收站
     *
     * @param requestParam 请求参数
     */

    default void saveRecycleBin(@RequestBody RecycleBinSaveReqDTO requestParam) {
        HttpUtil.post("http://localhost:8001/api/short-link/project/v1/recycle-bin/save", JSON.toJSONString(requestParam));
    }


    /**
     * 分页查询回收站短链接
     *
     * @param requestParam 分页查询短链接请求参数
     * @return 短链接分页查询响应
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("gidList",requestParam.getGidList());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());

        String resultPageStr = HttpUtil.get("http://localhost:8001/api/short-link/project/v1/recycle-bin/page", requestMap);

        // TypeReference - JSON反序列化时解析泛型
        return JSON.parseObject(resultPageStr, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>() {});
    }

    /**
     * 访问单个短链接指定时间内的监控数据
     *
     * @param fullShortUrl 短链接
     * @param gid 分组ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 短链接监控数据
     */
    default Result<ShortLinkStatsRespDTO> oneShortLinkStats(String fullShortUrl, String gid, String startDate, String endDate) {
        String resultPageStr = HttpUtil.get("http://localhost:8001/api/short-link/v1/stats?fullShortUrl=" + fullShortUrl + "&gid=" + gid + "&startDate=" + startDate + "&endDate=" + endDate);

        // TypeReference - JSON反序列化时解析泛型
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    }

    /**
     * 访问单个短链接指定时间内访问记录监控数据
     *
     * @param requestParam 请求参数
     * @return 短链接访问记录监控数据
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> shortLinkStatsAccessRecord(ShortLinkStatsAccessRecordReqDTO requestParam) {
        String resultPageStr = HttpUtil.get("http://localhost:8001/api/short-link/v1/stats/access-record", BeanUtil.beanToMap(requestParam));

        // TypeReference - JSON反序列化时解析泛型
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    }


    default Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(ShortLinkBatchCreateReqDTO requestParam) {
        String resultPageStr = HttpUtil.get("http://localhost:8001/api/short-link/admin/v1/create/batch", BeanUtil.beanToMap(requestParam));

        // TypeReference - JSON反序列化时解析泛型
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    };
}
